package com.giga.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class MailService {

    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送简单文本邮件
     */
    public void sendSimpleTextMail(String[] to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置抄送（可选），有普通抄送(setCc)和 盲抄送(setBcc)两种类型
        // 普通抄送邮件接收者可以看到邮件都抄送给了哪些人，而盲抄送邮件接收者则看不到
        message.setBcc(to);
        message.setSubject(subject);
        message.setText(content);
        message.setFrom(from);
        mailSender.send(message);
//        log.info("【文本邮件】成功发送！to={}", to);
    }

    /**
     * @param to      收件人多个
     * @param subject 主题
     * @param content 内容
     * @param imgPath 图片地址
     * @param imgId   图片在实体类中的变量名
     */
    public void sendImgSingle(String[] to, String subject, String content, String imgPath, String imgId)
            throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
        messageHelper.setFrom(from);
        messageHelper.setBcc(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(content, true);
        // 添加图片
        FileSystemResource fileResource = new FileSystemResource(new File(imgPath));
        messageHelper.addInline(imgId, fileResource);
        mailSender.send(mimeMessage);
//        log.info("【图片邮件】成功发送！to={}", to);
    }

    /**
     *
     * @param to        收件人多个
     * @param subject   主题
     * @param content   内容
     * @param imgPath   多张图片的多个地址
     * @param imgId     多个id
     * @throws MessagingException
     */
    public void sendImgMailMore(String to, String subject, String content, String[] imgPath, List imgId)
            throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
        messageHelper.setFrom(from);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(content, true);
        // 添加图片
        FileSystemResource fileResource = new FileSystemResource(new File("target/classes/static/img01/apple.jpg"));
        FileSystemResource fileResource1 = new FileSystemResource(new File("target/classes/static/apple.jpg"));
        messageHelper.addInline("test001", fileResource);
        messageHelper.addInline("test002",fileResource1);

        mailSender.send(mimeMessage);
//        log.info("【图片邮件】成功发送！to={}", to);
    }
    public void sendImgMailByMap(String to, String subject, String content, Map<String, String> imgMap)
            throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true);
        messageHelper.setFrom(from);
        messageHelper.setTo(to);
        messageHelper.setSubject(subject);
        messageHelper.setText(content, true);
        // 添加图片
        for (Map.Entry<String, String> entry : imgMap.entrySet()) {
            FileSystemResource fileResource = new FileSystemResource(new File(entry.getValue()));
            if (fileResource.exists()) {
                String filename = fileResource.getFilename();
                System.out.println(filename);
                messageHelper.addInline(entry.getKey(), fileResource);
            }
        }
        mailSender.send(mimeMessage);
        log.info("【图片邮件】成功发送！to={}", to);
    }
}
